package com.hyf.meterialdesign;

import android.app.Activity;
import android.app.ActivityOptions;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.hyf.meterialdesign.activity.AccentColorActivity;
import com.hyf.meterialdesign.activity.AppBarLayoutActivity;
import com.hyf.meterialdesign.activity.BaseCollaspingToolbarActivity;
import com.hyf.meterialdesign.activity.ExplodeAnimationActivity;
import com.hyf.meterialdesign.activity.FadeAnimationActivity;
import com.hyf.meterialdesign.activity.FunscreenFragmentActivity;
import com.hyf.meterialdesign.activity.RippleViewActivity;
import com.hyf.meterialdesign.activity.ShareViewActivity;
import com.hyf.meterialdesign.activity.SlideAnimationActivity;
import com.hyf.meterialdesign.activity.SystemBarTintActivity;
import com.hyf.meterialdesign.activity.ToolbarTintActivity;
import com.hyf.meterialdesign.activity.ToolbarTitleCenterActivity;
import com.hyf.meterialdesign.activity.TouchFeedbackActivity;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends BaseActivity implements AdapterView.OnItemClickListener{

    private ListView listview;
    private List<ListViewItem> items;
    private SimpleBaseAdapter adapter;
    private ImageView imageView;

    private Activity activity;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        activity = this;
    }

    @Override
    protected void initView() {
        listview = (ListView) findViewById(R.id.listview);
        imageView = (ImageView) findViewById(R.id.imageView);
    }

    @Override
    protected void initData() {
        items = new ArrayList<>();
        adapter = new SimpleBaseAdapter(this,items);
        listview.setAdapter(adapter);

        addItem("Actionbar沉浸式状态栏", new OnclickListener() {
            @Override
            public void click() {
                startActivity(new Intent(activity, SystemBarTintActivity.class));
            }
        });

        addItem("ToolBar沉浸式状态栏", new OnclickListener() {
            @Override
            public void click() {
                startActivity(new Intent(activity, ToolbarTintActivity.class));
            }
        });

        addItem("fragment的沉浸", new OnclickListener() {
            @Override
            public void click() {
                startActivity(new Intent(activity, FunscreenFragmentActivity.class));
            }
        });

        addItem("AppBarLayoutActivity", new OnclickListener() {
            @Override
            public void click() {
                startActivity(new Intent(activity, AppBarLayoutActivity.class));
            }
        });

        addItem("基础的CollaspingToolbarLayout", new OnclickListener() {
            @Override
            public void click() {
                startActivity(new Intent(activity, BaseCollaspingToolbarActivity.class));
            }
        });

        addItem("任意控件实现涟漪效果", new OnclickListener() {
            @Override
            public void click() {
                startActivity(new Intent(activity, RippleViewActivity.class));
            }
        });

        addItem("ToolbarTitleCenterActivity", new OnclickListener() {
            @Override
            public void click() {
                startActivity(new Intent(activity, ToolbarTitleCenterActivity.class));
            }
        });

        addItem("修改编辑框和Checkbox的颜色", new OnclickListener() {
            @Override
            public void click() {
                startActivity(new Intent(activity, AccentColorActivity.class));
            }
        });

        addItem("【5.0】触摸反馈效果", new OnclickListener() {
            @Override
            public void click() {
                startActivity(new Intent(activity, TouchFeedbackActivity.class));
            }
        });


        addItem("【5.0】分解进入动画", new OnclickListener() {
            @Override
            public void click() {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    startActivity(new Intent(activity, ExplodeAnimationActivity.class), ActivityOptions.makeSceneTransitionAnimation(activity).toBundle());
                }else {
                    startActivity(new Intent(activity,ExplodeAnimationActivity.class));
                }
            }
        });

        addItem("【5.0】淡入淡出进入动画", new OnclickListener() {
            @Override
            public void click() {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    startActivity(new Intent(activity, FadeAnimationActivity.class), ActivityOptions.makeSceneTransitionAnimation(activity).toBundle());
                }else {
                    startActivity(new Intent(activity,FadeAnimationActivity.class));
                }
            }
        });

        addItem("【5.0】滑动进入动画", new OnclickListener() {
            @Override
            public void click() {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    startActivity(new Intent(activity, SlideAnimationActivity.class), ActivityOptions.makeSceneTransitionAnimation(activity).toBundle());
                }else {
                    startActivity(new Intent(activity,SlideAnimationActivity.class));
                }
            }
        });


        addItem("【5.0】共享元素动画", new OnclickListener() {
            @Override
            public void click() {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    startActivity(new Intent(activity, ShareViewActivity.class), ActivityOptions.makeSceneTransitionAnimation(activity,imageView,"imageView").toBundle());
                }else {
                    startActivity(new Intent(activity,ShareViewActivity.class));
                }
            }
        });

        adapter.notifyDataSetChanged();
    }

    @Override
    protected void initListener() {
        listview.setOnItemClickListener(this);
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.LOLLIPOP){

                }
            }
        });
    }

    private void addItem(String title,OnclickListener listener) {
        items.add(new ListViewItem(title,listener));
    }

//    -------------------------------------------------------------------------华丽分割线-------------------------------------------------------------------------------
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        items.get(position).getListener().click();
    }

    class SimpleBaseAdapter extends BaseAdapter{
        private Activity activity;
        private List<ListViewItem> list;

        public SimpleBaseAdapter(Activity activity, List<ListViewItem> list) {
            this.activity = activity;
            this.list = list;
        }

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int position) {
            return list.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            TextView textView = (TextView) View.inflate(activity,android.R.layout.simple_list_item_1,null);
            textView.setText(addZero(position+1)+"."+list.get(position).getTitle());
            return textView;
        }
    }

    class ListViewItem{
        private String title;
        private OnclickListener listener;

        public ListViewItem(String title, OnclickListener listener) {
            this.title = title;
            this.listener = listener;
        }

        public String getTitle() {
            return title;
        }

        public void setTitle(String title) {
            this.title = title;
        }

        public OnclickListener getListener() {
            return listener;
        }

        public void setListener(OnclickListener listener) {
            this.listener = listener;
        }
    }

    interface OnclickListener{
        void click();
    }

    private String addZero(int num){
        return num<10 ? "0"+num : ""+num;
    }
    //    -------------------------------------------------------------------------华丽分割线-------------------------------------------------------------------------------
}
